﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace MultithreadFileCopy
{
    public class MultithreadFile : IEquatable<MultithreadFile>
    {
        public MultithreadFile(string SourceFile, string DestinationFile, bool overwrite = false)
        {
            SourcePath = SourceFile;
            DestinationPath = DestinationFile;
            Overwrite = overwrite;

            if (SourcePath.ToLower() == DestinationPath.ToLower())
                throw new Exception("SourceFile equals DestinationFile");

            if (!File.Exists(SourcePath))
                throw new Exception(string.Format("Invalid path: {0}", SourcePath));

            FileSize = new FileInfo(SourcePath).Length;

            Status = FileStatus.Awaiting;
        }

        public string SourcePath
        { get; private set; }
        public string DestinationPath
        { get; private set; }

        public long FileSize
        { get; private set; }
        public bool Overwrite
        { get; private set; }
        public FileStatus Status
        { get; private set; }
        public string Log
        { get; private set; }

        public void SetStatus(FileStatus STATUS)
        { Status = STATUS; }
        public void AddLog(string LogEntry)
        {
            if (String.IsNullOrEmpty(LogEntry))
                Log = "<------------ LOG ----------->";

            Log += "\n" + LogEntry;
        }

        public bool Equals(MultithreadFile FILE)
        {
            return DestinationPath.ToLower() == FILE.DestinationPath.ToLower();
        }
    }
    public class MultithreadFileEnumerator : IEnumerator<MultithreadFile>
    {
        private List<MultithreadFile> Items;
        private int Now = -1;

        public MultithreadFileEnumerator(IEnumerable<MultithreadFile> List)
        {
            Items = new List<MultithreadFile>(List);
        }

        public void Reset() { Now = -1; }
        public bool MoveNext()
        {
            Now++;
            return Now < Items.Count;
        }

        object IEnumerator.Current
        { get { return Items[Now]; } }
        public MultithreadFile Current
        { get { return Items[Now]; } }

        public void Dispose() { }
    }

    public enum FileStatus
    {
        Awaiting,
        Executing,
        Skiped,
        Error,
        Sucess,
    }
}
